package com.wy.springbootblog.controller;

import com.wy.springbootblog.pojo.Category;
import com.wy.springbootblog.pojo.Result;
import com.wy.springbootblog.pojo.SearchBean;
import com.wy.springbootblog.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    // 新增文章分类
    @PostMapping("/add")
    public Result<Void> add(@RequestBody @Validated Category category) {
        return categoryService.saveCategory(category);
    }

    // 删除文章分类
    @DeleteMapping("/delete/{id}")
    public Result<Void> delete(@PathVariable @NotNull(message = "分类id不能为空") Integer id) {
        return categoryService.deleteCategory(id);
    }

    // 查询文章分类列表
    @PostMapping("/list")
    public Result<List<Category>> list(@RequestBody SearchBean<Category> searchBean) {
        return categoryService.categoryList(searchBean);
    }

    // 修改文章分类
    @PostMapping("/update")
    public Result<Void> update(@RequestBody @Validated Category category) {
        return categoryService.updateCategory(category);
    }
}
